var vm_zhuanjia = new Vue({
    el: "#index",
    data: {
        deptid: $("#deptid").val(),
        olddeptid: $("#olddeptid").val(),
        jigoujibie: $("#jigoujibie").val(),
        roleid: $("#roleid").val(),


        danweichaoguanroleid:100,
        zjzxroleid:102,

        userlist: [],// 列表list

        //查询用字段
        loginName: "",
        phonenumber: "",
        email: "",

        //add字段
        addItem:{
            loginName: "",
            phonenumber: "",
            email: "",
            password:"",
            userName:"",
            text1:"",//所属部门
            text2:"",//办公电话
            remark:""
        },

        //edit字段
        editItem:{
            loginName: "",
            phonenumber: "",
            email: "",
            password:"",
            userName:"",
            text1:"",//所属部门
            text2:"",//办公电话
            remark:""
        }
    },
    created: function () {
        var _this = this;

        _this.bindUserList();

    },

    methods: {

        bindUserList: function () {
            var _this = this;
            $.post('/system/user/bendanweirenyuanguanliList', {
                deptid: _this.olddeptid,
                loginName:_this.loginName,
                phonenumber:_this.phonenumber,
                email:_this.email,
                rdm: Math.random()
            }, function (ppData) {
                if (ppData != null) {
                    var data = ppData.resultContent;
                    _this.userlist = data;
                }
            }, "json");
        },

        reset: function () {
            var _this = this;
            _this.loginName="",
            _this.phonenumber="",
            _this.email="",
            _this.bindUserList();
        },
        resetPwd:function (user) {
            var url = 'system/user/yonghuguanli/resetPwd/' + user.userId;
            $.modal.open("重置密码", url, '800', '300');
        },

        toAdd:function () {
            var  _this = this
            _this.addItem = {
                loginName: "",
                phonenumber: "",
                email: "",
                password:"",
                userName:"",
                text1:"",//所属部门
                text2:"",//办公电话
                remark:""
            }
            $("#myModalLabel1").html("新增本单位普通管理员");
            $("#addModal").modal();
        },
        toEdit:function (userItem) {
            var  _this = this
            _this.editItem = {
                phonenumber: userItem.phonenumber,
                email: userItem.email,
                sex:userItem.sex,
                userName:userItem.userName,
                loginName: userItem.loginName,
                userId:userItem.userId,
                text1:userItem.text1,//所属部门
                text2:userItem.text2,//办公电话
                remark:userItem.remark
            }
            $("#myModalLabel2").html("修改");
            $("#editModal").modal();
        },
        toChakan:function (userItem) {
            var  _this = this
            _this.editItem = {
                phonenumber: userItem.phonenumber,
                email: userItem.email,
                sex:userItem.sex,
                userName:userItem.userName,
                loginName: userItem.loginName,
                userId:userItem.userId,
                text1:userItem.text1,//所属部门
                text2:userItem.text2,//办公电话
                remark:userItem.remark
            }
            $("#myModalLabel3").html("查看");
            $("#chakanModal").modal();
        },
        add:function () {
            var _this = this;
            $("#form-user-add").validate({
                onkeyup: false,
                rules: {
                    loginName: {
                        minlength: 2,
                        maxlength: 20,
                        remote: {
                            url:  "/system/user/checkLoginNameUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "loginName": function () {
                                    return $.common.trim(_this.addItem.loginName);
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                    password: {
                        required:true,
                        minlength: 5,
                        maxlength: 20
                    },
                    email: {
                        email: true,
                        remote: {
                            url:  "/system/user/checkEmailUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "email": function () {
                                    return $.common.trim(_this.addItem.email);
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                    phonenumber: {
                        isPhone: true,
                        remote: {
                            url:  "/system/user/checkPhoneUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "phonenumber": function () {
                                    return $.common.trim(_this.addItem.phonenumber);
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                },
                messages: {
                    "loginName": {
                        remote: "用户已经存在"
                    },
                    "email": {
                        remote: "Email已经存在"
                    },
                    "phonenumber": {
                        remote: "手机号码已经存在"
                    }
                },
                focusCleanup: true
            });
            if ($.validate.form("form-user-add") && checkpwd(0, _this.addItem.password)) {
                $.post('/system/user/ModalAdd', {
                    olddeptId: _this.olddeptid,
                    loginName:_this.addItem.loginName,
                    phonenumber:_this.addItem.phonenumber,
                    email:_this.addItem.email,
                    password:_this.addItem.password,
                    userName:_this.addItem.userName,
                    roleIds:_this.jigoujibie=="0"?"104":"105",
                    text1:_this.addItem.text1,
                    text2:_this.addItem.text2,
                    remark:_this.addItem.remark,
                    rdm: Math.random()
                }, function (ppData) {
                    if(ppData.code==0){
                        $("#addModal").modal("hide");
                        _this.addItem = {
                            loginName: "",
                            phonenumber: "",
                            email: "",
                            password:"",
                            userName:"",
                            text1:"",//所属部门
                            text2:"",//办公电话
                            remark:""
                        }
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "添加成功!",
                        });
                        _this.bindUserList()
                    }else {
                        layer.alert(ppData.msg)
                    }
                }, "json");
            }

        },
        edit:function () {
            var _this = this;
            var prefix = "/system/user";
            $("#form-user-edit").validate({
                onkeyup: false,
                rules:{
                    userName:{
                        required:true
                    },
                    email:{
                        email:true,
                        remote: {
                            url: prefix + "/checkEmailUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "userId": function() {
                                    return _this.editItem.userId;
                                },
                                "email": function() {
                                    return $.common.trim(_this.editItem.email);
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                    phonenumber:{
                        isPhone:true,
                        remote: {
                            url: prefix + "/checkPhoneUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "userId": function() {
                                    return _this.editItem.userId;
                                },
                                "phonenumber": function() {
                                    return $.common.trim(_this.editItem.phonenumber);
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                },
                messages: {
                    "email": {
                        remote: "Email已经存在"
                    },
                    "phonenumber":{
                        remote: "手机号码已经存在"
                    }
                },
                focusCleanup: true
            });
            if ($.validate.form("form-user-edit") && checkpwd(0, _this.editItem.password)) {
                $.post('/system/user/ModalEdit', {
                    olddeptId: _this.olddeptid,
                    userId:_this.editItem.userId,
                    phonenumber:_this.editItem.phonenumber,
                    email:_this.editItem.email,
                    userName:_this.editItem.userName,
                    loginName:_this.editItem.loginName,
                    text1:_this.editItem.text1,//所属部门
                    text2:_this.editItem.text2,//办公电话
                    remark:_this.editItem.remark,
                    rdm: Math.random()
                }, function (ppData) {
                    if(ppData.code==0){
                        $("#editModal").modal("hide");
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "修改成功!",
                        });
                        _this.bindUserList()
                    }else {
                        layer.alert(ppData.msg)
                    }
                }, "json");
            }
        },
        Remove:function (userId) {
            var _this = this
            $.modal.confirm("确认删除", function() {
                $.operate.get("/system/user/ModalRemove?userId="+userId,function () {
                    _this.bindUserList()
                })

            });
        }
    },
})